package com.tyson.member.controller;


import com.tyson.common.utils.R;
import com.tyson.member.entity.BlAdmin;
import com.tyson.member.entity.vo.LoginParamVo;
import com.tyson.member.service.BlAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    BlAdminService blAdminService;

    @PostMapping("login")
    public R login(@RequestBody LoginParamVo vo) {
        String token = blAdminService.login(vo);
        if (token != null) {
            return R.ok().data("token", token);
        } else {
            return R.error();
        }
    }

    @GetMapping("info")
    public R info (@RequestParam("token") String token){
       BlAdmin userInfo = blAdminService.getInfo(token);
        if (StringUtils.isEmpty(userInfo)){
            return R.ok().data("roles","[Admin]").data("avatar",userInfo.getHeader()).data("name",userInfo.getUsername());
        }else {
            return R.error();
        }
    }
}
